package exam.axhu.action;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

import exam.axhu.entity.ShortAnswer;
import exam.axhu.service.ShortAnswerService;


@SuppressWarnings("serial")
@Controller @Scope("prototype")
public class ShortAnswerAction extends ActionSupport{
//defaultClass: shortAnswerLogin
	@Resource ShortAnswerService shortAnswerService;

	private ShortAnswer shortAnswer;
	public ShortAnswer getShortAnswer() {
		return shortAnswer;
	}
	public void setShortAnswer(ShortAnswer shortAnswer) {
		this.shortAnswer = shortAnswer;
	}
	
	
	public String editShortAnswerA(){
		ActionContext.getContext().put("editshortAnswer", shortAnswerService.findByID(shortAnswer.getShortAnswerID()));
		return "Edit";
	}
	

	public String editShortAnswerB(){
		ShortAnswer newshortAnswer = shortAnswerService.findByID(shortAnswer.getShortAnswerID());
		newshortAnswer.setShortAnswerContent(shortAnswer.getShortAnswerContent());
		newshortAnswer.setShortAnswerAnswer(shortAnswer.getShortAnswerAnswer());
		shortAnswerService.update(newshortAnswer);
		addActionMessage("修改成功!欢迎!");
		ActionContext.getContext().put("shortAnswers", shortAnswerService.list());
		return "EditSucceed";
	}
	
	public String editShortAnswers(){
		ActionContext.getContext().put("shortAnswers", shortAnswerService.list());
		return "ShortAnswerInfo";
	}

	
	public String AddShortAnswerA(){
		return "AddShortAnswer";
	}
	
	public String AddShortAnswerB(){
	    
		if(shortAnswer.getShortAnswerContent().length()!=0 &&shortAnswer.getShortAnswerAnswer().length()!=0)
		{
			
			shortAnswerService.save(shortAnswer);
			addActionMessage("添加成功!欢迎!");
			ActionContext.getContext().put("shortAnswers", shortAnswerService.list());
			return "AddSucceed";
		}
				
		addActionError("题目内容、选项、答案不能为空!");
		return "AddFailed";
	}

	public String DeleteShortAnswerA(){
		ActionContext.getContext().put("deleteShortAnswer", shortAnswerService.findByID(shortAnswer.getShortAnswerID()));
		return "DeleteShortAnswer";
	}
	
	public String DeleteShortAnswerB(){
		if(shortAnswer!=null)
			shortAnswerService.delete(shortAnswer.getShortAnswerID());
		ActionContext.getContext().put("shortAnswers", shortAnswerService.list());
		return "DeleteSucceed";
	}
	
}
